/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package update;


import GroupBy.*;
import HAVING.*;
import java.util.*;
import java.util.regex.*;
import ParseWhere.*;

/**
 *
 * @author Dung Doan
 */
public class UpdateParse {
    
    
    private static String getUpdate(String root)
    {
        String query = root.toUpperCase();
        int beginIndex = 0;
        int endIndex = 0;
        endIndex = query.indexOf("UPDATE", beginIndex);
        if (endIndex < beginIndex) return null;
        beginIndex = endIndex + 6;
        endIndex = query.indexOf("SET", beginIndex);
        if (endIndex < beginIndex)
            return null;
        return root.substring(beginIndex, endIndex).trim();
    }
    
    private static String getSet(String root)
    {
        String query = root.toUpperCase();
        int beginIndex = 0;
        int endIndex = 0;
        endIndex = query.indexOf("UPDATE", beginIndex);
        if (endIndex < beginIndex) return null;
        beginIndex = endIndex + 6;
        endIndex = query.indexOf("SET", beginIndex);
        if (endIndex < beginIndex)
            return null;
        beginIndex = endIndex + 3;
        endIndex = query.indexOf("WHERE", beginIndex);
        if (endIndex < beginIndex)
            return root.substring(beginIndex).trim();
        return root.substring(beginIndex, endIndex).trim();
    }
    
    private static String getWhere(String root)
    {
        String query = root.toUpperCase();
        int beginIndex = 0;
        int endIndex = 0;
        endIndex = query.indexOf("UPDATE", beginIndex);
        if (endIndex < beginIndex) return null;
        beginIndex = endIndex + 6;
        endIndex = query.indexOf("SET", beginIndex);
        if (endIndex < beginIndex)
            return null;
        beginIndex = endIndex + 3;
        endIndex = query.indexOf("WHERE", beginIndex);
        if (endIndex < beginIndex)
            return null;
        return root.substring(endIndex).trim();
        
        
    }
    
    private static Boolean QueryIsValid(String query)
    {
        int endIndex = query.indexOf(">=");
        if (endIndex >= 0)
            return false;
        endIndex = query.indexOf("<=");
        if (endIndex >= 0)
            return false;
        endIndex = query.indexOf("<");
        if (endIndex >= 0)
            return false;
        endIndex = query.indexOf(">");
        if (endIndex >= 0)
            return false;
        
        if (query.indexOf("==") >= 0)
            return false;
        
        
        if (query.indexOf("=>") >= 0)
            return false;
        if (query.indexOf("=<") >= 0)
            return false;        
        return true;
        
    }
    private static ExpressionNode String2ExpressionNode(String str)
    {
        
        int beginIndex = 0;

        if (QueryIsValid(str) == false)
            return null;
        
        int endIndex = str.indexOf("=", beginIndex);
        if (endIndex < beginIndex)
            return null;
        String attribute = str.substring(beginIndex, endIndex).trim();
        beginIndex = endIndex + 1;
        String value = str.substring(beginIndex).trim();
        
        
        return new ExpressionNode(attribute, "=", value);
        
    }
    private static List<ExpressionNode> getOperands(String query)
    {
        ArrayList<String> operands = new ArrayList<String>(); 
        String REGEX = ",";
        Pattern p = Pattern.compile(REGEX);
        String[] items = p.split(query);
        for (int i = 0 ; i < items.length ; i++)
            operands.add(items[i].trim());
        
        
        ArrayList<ExpressionNode> expNodeArr = new ArrayList<ExpressionNode>();
        for (int i = 0 ; i < operands.size() ; i++)
            expNodeArr.add(String2ExpressionNode(operands.get(i)));
        return expNodeArr;
        
    }
    private static Boolean setQueryIsValid(ArrayList<ExpressionNode> list)
    {
        for (int i = 0 ; i < list.size() ; i++)
            if (list.get(i) == null)
                return false;
        return true;
    }
    private static Boolean executeUpdateQuery(String query, String database) 
    {	
        // Xu ly table
        String tableName = getUpdate(query); // Lay ten bang
        if (tableName == null) // cau lenh khong hop le
            return false;
        // Xu ly doan SET gia tri
        if (getSet(query) == null || getWhere(query) == null)
            return false;
        ArrayList<ExpressionNode> en = (ArrayList<ExpressionNode>) getOperands(getSet(query));

        if (setQueryIsValid(en) == false) // Neu trong set co nguoi nao go tuoi > 21 ==> false
            return false;
        
                

        
        
        
        return true;
    }
    public static void main(String[] args)
    {
        String query = "select hocsinh "
                + "from ten = 'Dung', tuoi = 21"
                + "WHERE hocsinh.id = '0912074'"
                + "group by ten, tuoi"
                + "having sum(tuoi) >= 19"
                + "order by hs.tuoi asc, hs.ngaysinh desc";
        String query1 = "select hocsinh "
                + "from ten = 'Dung', tuoi = 21"
                + "WHERE hocsinh.id = '0912074'"
                
                
                + "order by hs.tuoi asc, hs.ngaysinh desc";
        String query2 = "select hocsinh.ten from hocsinh where hocsinh.id = '0912074' group by ten having tuoi >= 19 order by hs.tuoi, hs.ngaysinh desc";
//        System.out.println(getUpdate(query));
//     
//        System.out.println(getSet(query));
//        System.out.println(getWhere(query));
//        ArrayList<ExpressionNode> en = (ArrayList<ExpressionNode>) getOperands(getSet(query));
//        
//        for (int i = 0 ; i < en.size() ; i++)
//            en.get(i).Display();
        //System.out.println(executeUpdateQuery(query, "KHTN"));
        OrderBy ob = new OrderBy();
        GroupBy gb = new GroupBy();
        
        
        
        // XAI ORDER BY
        ArrayList<OrderByType> list = (ArrayList<OrderByType>) ob.ParseOrderBy(query2);
        
        for (int i = 0 ; i < list.size() ; i++)
            System.out.println(list.get(i).getM_table() + "." + list.get(i).getM_attribute() + "." + list.get(i).getM_isDesc());
        
        // XAI GROUP BY
        ArrayList<String> list1 = (ArrayList<String>) gb.ParseGroupBy(query);
        for (int i = 0 ; i < list1.size() ; i++)
            System.out.println(list1.get(i));
        
        // XAI HAVING
        Having h = new Having();
        HavingType ht = h.ParseHaving(query);
        System.out.println(ht.getM_func());
        System.out.println(ht.getM_attribute());
        System.out.println(ht.getM_operator());
        System.out.println(ht.getM_value());
        
        
        
    }
}
